package com.ruoyi.kenuo.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kenuo.domain.KernelVerison;
import com.ruoyi.kenuo.service.IKernelVerisonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 版本控制Controller
 *
 * @author polo
 * @date 2022-12-05
 */
@Api(tags = "版本控制")
@RestController
@RequestMapping("/kenuo/verison")
public class KernelVerisonController extends BaseController
{
    @Autowired
    private IKernelVerisonService kernelPendiVerisonService;

    /**
     * 查询版本控制列表
     */
    @ApiOperation(value = "查询版本控制列表（带分页）",notes = "查询版本控制列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "versionNum", value = "版本号", required = false, dataType = "String")
    })
    @GetMapping("/list")
    public TableDataInfo list(KernelVerison kernelPendiVerison)
    {
        startPage();
        List<KernelVerison> list = kernelPendiVerisonService.selectKernelVerisonList(kernelPendiVerison);
        return getDataTable(list);
    }

    /**
     * 导出版本控制列表
     */
    @PreAuthorize("@ss.hasPermi('kenuo:verison:export')")
    @Log(title = "版本控制", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KernelVerison kernelPendiVerison)
    {
        List<KernelVerison> list = kernelPendiVerisonService.selectKernelVerisonList(kernelPendiVerison);
        ExcelUtil<KernelVerison> util = new ExcelUtil<KernelVerison>(KernelVerison.class);
        util.exportExcel(response, list, "版本控制数据");
    }

    /**
     * 获取版本控制详细信息
     */
    @PreAuthorize("@ss.hasPermi('kenuo:verison:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(kernelPendiVerisonService.selectKernelVerisonById(id));
    }

    /**
     * 新增版本控制
     */
    @PreAuthorize("@ss.hasPermi('kenuo:verison:add')")
    @Log(title = "版本控制", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KernelVerison kernelPendiVerison)
    {
        //查询版本号是否重复
        String vnum = kernelPendiVerison.getVersionNum();
        String vtype = kernelPendiVerison.getRemarkOne();
        if(StringUtils.isNotEmpty(vnum)&&StringUtils.isNotEmpty(vtype)){
            KernelVerison vs = new KernelVerison();
            vs.setRemarkOne(vtype);
            vs.setVersionNum(vnum);
            List<KernelVerison> vsList = kernelPendiVerisonService.selectKernelVerisonList(vs);
            if(vsList.size()>0){
                return AjaxResult.error("The same version-Number have been existed");
            }
        }else {
            return AjaxResult.error("Missing parameter,please check it again");
        }
        return toAjax(kernelPendiVerisonService.insertKernelVerison(kernelPendiVerison));
    }

    /**
     * 修改版本控制
     */
    @PreAuthorize("@ss.hasPermi('kenuo:verison:edit')")
    @Log(title = "版本控制", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KernelVerison kernelPendiVerison)
    {
        //查询版本号是否重复
        String vnum = kernelPendiVerison.getVersionNum();
        String vtype = kernelPendiVerison.getRemarkOne();
        if(StringUtils.isNotEmpty(vnum)&&StringUtils.isNotEmpty(vtype)){
            KernelVerison vs = new KernelVerison();
            vs.setRemarkOne(vtype);
            vs.setVersionNum(vnum);
            List<KernelVerison> vsList = kernelPendiVerisonService.selectKernelVerisonList(vs);
            if(vsList.stream().anyMatch(x->x.getId().equals(kernelPendiVerison.getId()))){
                return AjaxResult.error("The same version-Number have been existed");
            }
        }else {
            return AjaxResult.error("Missing parameter,please check it again");
        }
        return toAjax(kernelPendiVerisonService.updateKernelVerison(kernelPendiVerison));
    }

    /**
     * 删除版本控制
     */
    @PreAuthorize("@ss.hasPermi('kenuo:verison:remove')")
    @Log(title = "版本控制", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(kernelPendiVerisonService.deleteKernelVerisonByIds(ids));
    }
}
